﻿/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace MongoPI
{
    /// <summary>
    /// MongoDB资源管理器树型视图
    /// </summary>
    public class ExplorerView : TreeView
    {
        /// <summary>
        /// 视图节点
        /// </summary>
        public List<ExplorerViewItem> MongoList = new List<ExplorerViewItem>();

        /// <summary>
        /// MongoDB资源管理器树型视图构造函数
        /// </summary>
        public ExplorerView()
        {
            ItemsSource = MongoList;
            return;
        } // Function ExplorerView()

        /// <summary>
        /// 添加服务器连接
        /// </summary>
        public void ConnectAdd(ServerConnectInfo info)
        {
            MongoClient mongoClient = new MongoClient(info.ConnectString);
            var dbNmaeList = mongoClient.ListDatabaseNames().ToList();
            if (dbNmaeList.Count <= 0)
            {
                MessageBox.Show("连接失败！");
                return;
            }

            // 如果服务已经添加，则直接返回
            if (MongoList.Any<ExplorerViewItem>(p => p.Name == info.ServerName))
            {
                return;
            }

            // 添加服务器连接
            var serverItem = new ExplorerViewItem(null, ExplorerViewItem.ItemType.Server, info.ServerName, "/Images/IconServer.png");
            serverItem.BindObject = mongoClient;
            MongoList.Add(serverItem);

            // 添加服务器数据库节点
            foreach (string dbName in dbNmaeList)
            {
                DatabaseAdd(mongoClient, dbName, serverItem);
            }

            return;
        } // Function ConnectAdd()

        /// <summary>
        /// 添加数据库节点
        /// </summary>
        /// <param name="mongoClient">Mongo客户端</param>
        /// <param name="dbName">数据库</param>
        /// <param name="parent">父节点</param>
        public void DatabaseAdd(MongoClient mongoClient, string dbName, ExplorerViewItem parent)
        {
            // 获得数据库
            IMongoDatabase database = mongoClient.GetDatabase(dbName);
            if (null == database)
            {
                return;
            }

            // 如果数据库已经添加，则直接返回
            if (parent.Children.Any<ExplorerViewItem>(p => p.Name == dbName))
            {
                return;
            }

            // 添加数据库节点
            var databaseItem = new ExplorerViewItem(parent, ExplorerViewItem.ItemType.Database, dbName, "/Images/IconDatabase.png");
            databaseItem.BindObject = database;
            parent.Children.Add(databaseItem);

            // 填充集合节点
            FillCollectionList(database, databaseItem);

            // 填充视图节点
            FillViewList(database, databaseItem);

            #region 用户节点
            // 添加集合节点
            var userItems = new ExplorerViewItem(parent, ExplorerViewItem.ItemType.UserList, "ItemUsers".Resource(), "/Images/IconUsers.png");
            userItems.BindObject = database;
            databaseItem.Children.Add(userItems);
            #endregion

            #region 角色节点
            // 添加集合节点
            var roleItems = new ExplorerViewItem(parent, ExplorerViewItem.ItemType.RoleList, "ItemRoles".Resource(), "/Images/IconRole.png");
            userItems.BindObject = database;
            databaseItem.Children.Add(roleItems);
            #endregion
            return;
        } // Function DatabaseAdd()

        /// <summary>
        /// 填充集合节点
        /// </summary>
        /// <param name="database"></param>
        /// <param name="databaseItem"></param>
        private void FillCollectionList(IMongoDatabase database, ExplorerViewItem databaseItem)
        {
            // 添加集合节点
            var collectionItems = new ExplorerViewItem(databaseItem, ExplorerViewItem.ItemType.CollectionList, "ItemCollections".Resource(), "/Images/IconFolderOpen.png");
            collectionItems.BindObject = database;
            databaseItem.Children.Add(collectionItems);

            // 添加集合子节点
            foreach (string name in database.ListCollectionNames().ToList())
            {
                CollectionAdd(database, collectionItems, name);
            }

            return;
        } // Function FillCollectionList()

        /// <summary>
        /// 填充视图节点
        /// </summary>
        /// <param name="database"></param>
        /// <param name="databaseItem"></param>
        private void FillViewList(IMongoDatabase database, ExplorerViewItem databaseItem)
        {
            var viewItems = new ExplorerViewItem(databaseItem, ExplorerViewItem.ItemType.ViewList, "ItemViews".Resource(), "/Images/IconFolderOpen.png");
            viewItems.BindObject = database;
            databaseItem.Children.Add(viewItems);

            return;
        } // Function FillViewList()

        /// <summary>
        /// 添加集合节点
        /// </summary>
        /// <param name="database">数据库</param>
        /// <param name="parent">父节点</param>
        public void CollectionAdd(IMongoDatabase database, ExplorerViewItem parent, string name)
        {
            // 如果集合已经添加，则直接返回
            if (parent.Children.Any<ExplorerViewItem>(p => p.Name == name))
            {
                return;
            }

            var item = new ExplorerViewItem(parent, ExplorerViewItem.ItemType.Collection, name, "/Images/IconCollection.png");
            item.BindObject = database;
            parent.Children.Add(item);

            return;
        } // Function CollectionAdd()

    } // Class ExplorerView
} // Namespace MongoPI
